#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

__author__ = "Yunpeng Li <ypli.chn@outlook.com.com>"

from  EU.admin import BroadcastBox
from  EU.admin.StatusCode import StatusCode
from  django.db import DatabaseError
from EU.models import News
from EU.models import Spot
from EU.models import SpotType
import logging
from django.utils import timezone
import json
from EU.admin.BroadcastBox import BroadcastBox
def post_news(date,title,author,content):
    """
    发布新闻
    :param date:    时间
    :param title:   标题
    :param author:  作者
    :param content: 内容
    :return:
    """
    result = {}
    try:
        news=News()
        news.date=timezone.now()
        news.content=content
        news.author=author
        news.title=title
        news.save()
    except DatabaseError as e:
        logging.error("数据库错误:",e)
        result['status'] = StatusCode.DBError
        result['message'] = "数据库错误"
    except Exception as e:
        logging.error("未知错误:",e)
        result['status'] = StatusCode.UnknowError
        result['message'] = "未知错误"
    else:
        result['status'] = StatusCode.Success
        result['message'] = "Success"
        result['news'] = {
            "id":news.id,
            "date":news.date.timestamp(),
            "author":news.author,
            "title":news.title,
            "content":news.content,
        }
    return json.dumps(result)


def update_news(nid,date,title,author,content):
    result = {}
    try:
        news = News.objects.get(id=nid)
        if(news== None):
             #result['status'] = StatusCode.Success
            result['message'] = "找不到这条新闻"
            return result
            news=News()
            news.id=nid
        news.date=timezone.now()
        news.title=title
        news.author=author
        news.content=content
        news.save()
        result["news"]={"date":news.date.timestamp()


        ,"title":news.title,"author":author,"content":news.content}

    except Exception as e:
        logging.error("数据库错误:",e)
        result['status'] = StatusCode.DBError
        result['message'] = "数据库错误"
    else:
        result['status'] = StatusCode.Success
        result['message'] = "Success"
    return json.dumps(result)


def delete_news(nid):
    result = {}
    try:
        news=News.objects.get(id=nid)
        news.delete()
    except DatabaseError as e:
        logging.error("数据库错误:",e)
        result['status'] = StatusCode.DBError
        result['message'] = "数据库错误"
    except Exception as e:
        result['status'] = StatusCode.UnknowError
        result['message'] = "数据库错误"
    else:
        result['status'] = StatusCode.Success
        result['message'] = "Success"
    return json.dumps(result)



def update_spot(sid,name,type,longitude,latitude,image,score,introduction,voice):
    result = {}
    try:
        spot = Spot.objects.get(id=sid)
        if(spot == None):
            spot=Spot()
        spot.name=name
        spot.date=timezone.now()
        spot.picture=image
        spot.score=score
        spot.introduction=introduction
        spot.voice_introduce=voice
        spot.spot_type_id=type
        spot.save()

        spot.picture=((str(spot.picture).split("/"))[-1])
        spot.voice_introduce=((str(spot.voice_introduce).split("/"))[-1])

        spot.save()

    except Spot.DoesNotExist:
        spot=Spot()
        spot.name=name
        spot.date = timezone.now()
        spot.latitude=latitude
        spot.longitude=longitude
        spot.picture=image
        spot.score=score
        spot.introduction=introduction
        spot.voice_introduce=voice
        spot.spot_type_id=type
        spot.save()
        spot.picture=((str(spot.picture).split("/"))[-1])
        spot.voice_introduce=((str(spot.voice_introduce).split("/"))[-1])
        spot.save()
    except Exception as e:
        logging.error("数据库错误:",e)
        result['status'] = StatusCode.DBError
        result['message'] = "数据库错误"
    else:
        result['status'] = StatusCode.Success
        result['message'] = "Success"
    return json.dumps(result)

def post_broadcast(admin_id,message,time_length):
    result = {}
    box = BroadcastBox()
    try:
        box.post_broadcast(admin_id,message,time_length)
    except Exception as e:
        logging.error("数据库错误:",e)
        result['status'] = StatusCode.DBError
        result['message'] = "数据库错误"
    else:
        result['status'] = StatusCode.Success
        result['message'] = "Success"
    return json.dumps(result)

def post_broadcast(admin_id,message,time_length):
    result = {}
    broadcast = None
    box = BroadcastBox()
    try:

        broadcast = box.post_broadcast(admin_id,message,int(time_length))
    except DatabaseError as e:
        print("数据库错误:",e)
        result['status'] = StatusCode.DBError
        result['message'] = "数据库错误"
    except Exception as e:
        print("未知错误:",e)
        result['status'] = StatusCode.UnknowError
        result['message'] = "未知错误"
    else:
        result['status'] = StatusCode.Success
        result['message'] = "Success"
        result['broadcast'] = {
            "id":broadcast.id,
            "date":broadcast.date.timestamp(),
            "message":broadcast.message,
            "poster":broadcast.admin.username
        }
    return json.dumps(result)


def delete_broadcast(broadcast_id):
    result = {}
    try:
        BroadcastBox().delete_broadcast(int(broadcast_id))
    except Exception as e:
        print(e)
        result['status'] = StatusCode.UnknowError
        result['message'] = "未知错误"
    else:
        result['status'] = StatusCode.Success
        result['message'] = "Success"

    return json.dumps(result)



def update_spot_type(type_id,is_open):
    result = {}
    type_id = int(type_id)

    is_open = int(is_open)
    if int(is_open) == 0:
        is_open = 0
    else:
        is_open = 1

    if type_id == 1:
        result = {
            "status":StatusCode.IllegalAttribute,
            "message":"参数错误,不可取消景点显示"
        }
        return json.dumps(result)
    try:
        spot_type = SpotType.objects.get(id=type_id)
        spot_type.is_open = is_open
        print(spot_type.is_open,spot_type.type,spot_type.id)
        spot_type.save()
    except DatabaseError as e:
        result = {
            "status":StatusCode.DBError,
            "message":"数据库错误"
        }
    except Exception as e:
        print(e)
        result = {
            "status":StatusCode.UnknowError,
            "message":"未知错误"
        }
    else:
        result = {
            "status":StatusCode.Success,
            "message":"success"
        }
    return json.dumps(result)
